package com.qf.servlet;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;


@WebServlet("/user")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    private void findOneByUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        String phone = req.getParameter("phone");
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");
        User user = new User(0, phone, password1, null, null);
        //添加用户数据到数据库
        String result = "";
        if (phone.length() != 11) {
            result = "falsePhone";
        } else if (!password1.equals(password2)) {
            result = "falsePassword";
        } else {
            result = "true";
            userService.add(user);
        }
        writeHTML(result, resp);
    }

    //登录
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        String phone = req.getParameter("phone");
        String password1 = req.getParameter("password1");
        User user = new User(0, phone, password1, null, null);
        String result = "";

        User user1 = userService.login(user);
        if (user1 == null) {
            result = "false";
        } else {
            result = "true";
            int id = user1.getId();
            HttpSession session = req.getSession();
            session.setAttribute("id", id);
            session.setMaxInactiveInterval(30 * 60 * 60);
        }
        writeHTML(result, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String idNum = req.getParameter("id_num");
        String username = req.getParameter("username");
        int id = getSession(req, resp);
        User user = new User(id,phone,password,idNum,username);
        boolean result = userService.update(user);
        String a = "";
        if (result) {
            a = "true";
        }else {
            a = "false";
        }
        writeHTML(a,resp);
    }

    private void findOne(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = getSession(req, resp);
        User oneById = userService.findOneById(id);
        if (oneById.getUsername() == null){
            oneById.setUsername("未设置");
        }
        if (oneById.getId_num() == null){
            oneById.setId_num("未设置");
        }
        writeHTML(oneById,resp);
    }
}